package com.hk.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.core.roleResult.PageRoleResult;
import com.hk.domain.SysRole;
import com.hk.Roledto.RoleDto;
import com.hk.mapper.RoleMapper;
import com.hk.service.RoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    private  final Integer OPEN_ROLE_STA=1;
    private  final Integer ROLE_STA=0;
    @Override
    public PageRoleResult list(RoleDto roleDto) {
        Page<SysRole> sysRolePage = new Page<>(roleDto.getPageNum(), roleDto.getPageSize());
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(roleDto.getRoleKey()!=null,SysRole::getRoleKey,roleDto.getRoleKey());
        wrapper.eq(roleDto.getRoleName()!=null,SysRole::getRoleName,roleDto.getRoleName());
        wrapper.eq(roleDto.getRoleKey()!=null,SysRole::getStatus,roleDto.getStaus());
        if (roleDto.getParams()!=null){
            wrapper.between(SysRole::getCreateTime, LocalDate.parse(roleDto.getParams().get("beginTime").toString()),LocalDate.parse(roleDto.getParams().get("endTime").toString()));}
        Page<SysRole> page = roleMapper.selectPage(sysRolePage, wrapper);
        PageRoleResult pageResult = new PageRoleResult();
        pageResult.setRows(page.getRecords());
        pageResult.setTotal((int)page.getTotal());
        pageResult.setMsg("查询成功");
        pageResult.setCode(200);
        return pageResult;



    }

    @Override
    public void changeStatus(SysRole sysRole) {
        UpdateWrapper<SysRole> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id",sysRole.getRoleId()).set("status",(sysRole.getStatus()==OPEN_ROLE_STA)?ROLE_STA:OPEN_ROLE_STA);

    }

    @Override
    public SysRole selectById(Integer roleId) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleId,roleId);

        return roleMapper.selectOne(wrapper);
    }

    @Override
    public void deleteById(Integer roleId) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleId,roleId);
        roleMapper.delete(wrapper);
    }
}
